package neu.edu.cn.nuemall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import neu.edu.cn.nuemall.dto.BooksDTO;
import neu.edu.cn.nuemall.dto.BooksDescriptionDTO;
import neu.edu.cn.nuemall.dto.BooksSkuDTO;
import neu.edu.cn.nuemall.dto.BooksSkuImgDTO;

import neu.edu.cn.nuemall.entity.*;
import neu.edu.cn.nuemall.mapper.*;
import neu.edu.cn.nuemall.service.BooksService;
import neu.edu.cn.nuemall.service.PublishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BooksServiceImpl implements BooksService {

    @Autowired
    private BooksMapper booksMapper;

    @Autowired
    private BooksSkuMapper booksSkuMapper;

    @Autowired
    private BooksSkuImgMapper booksSkuImgMapper;

    @Autowired
    private PublishService publishService;

    @Autowired
    private BooksTypeRelationMapper booksTypeRelationMapper;

    @Autowired
    private BooksDescriptionMapper booksDescriptionMapper;


    @Override
    public List<BooksDTO> listLatestBooks() {
        QueryWrapper<Books> queryWrapper=new QueryWrapper();
        queryWrapper.orderByDesc("create_at");

        List<Books> booksList= booksMapper.selectList(queryWrapper);
        List<BooksDTO> booksDTOList =booksList.stream().map(books ->{
            return getBooksById(books.getId());
        }).collect(Collectors.toList());
        return booksDTOList;
    }

    @Override
    public BooksDTO getBooksById(String id) {
        Books books = booksMapper.selectById(id);
        BooksDTO booksDTO = new BooksDTO();
        BeanUtils.copyProperties(books, booksDTO);

        booksDTO.setBooksSkuList(listBooksSku(id));
        booksDTO.setBelongpublish(publishService.getPublishById(books.getBelongPublishId()));

        // 查询商品描述信息对象
        QueryWrapper<BooksDescription> booksDescriptionQueryWrapper = new QueryWrapper<>();
        booksDescriptionQueryWrapper.eq("books_id", id);
        BooksDescription booksDescription = booksDescriptionMapper.selectOne(booksDescriptionQueryWrapper);
        BooksDescriptionDTO booksDescriptionDTO = new BooksDescriptionDTO();
        BeanUtils.copyProperties(booksDescription, booksDescriptionDTO);
        booksDTO.setBooksDescription(booksDescriptionDTO);

        return booksDTO;
    }

    @Override
    public List<BooksDTO> searchBooks(String keyword, String keyword2) {
        QueryWrapper<Books> queryWrapper=new QueryWrapper();
        queryWrapper.like("name",keyword);
        queryWrapper.like("author",keyword2);

        queryWrapper.orderByDesc("create_at");

        List<Books> booksList= booksMapper.selectList(queryWrapper);
        List<BooksDTO> booksDTOList =booksList.stream().map(books ->{
            return getBooksById(books.getId());
        }).collect(Collectors.toList());

        return booksDTOList;
    }

    @Override
    public PageInfo<BooksDTO> listPublishBooks(Integer page, String publishId, String typeId) {

        List<String> booksIdList=new ArrayList<String>();

        if (StringUtils.hasText(typeId)){
            QueryWrapper<BooksTypeRelation> queryWrapper=new QueryWrapper<BooksTypeRelation>();
            queryWrapper.eq("books_type_id",typeId);
            List<BooksTypeRelation> booksTypeRelationList= booksTypeRelationMapper.selectList(queryWrapper);

            booksIdList=booksTypeRelationList.stream()
                    .map(booksTypeRelation -> booksTypeRelation.getBooksId())
                    .collect(Collectors.toList());

        }

        QueryWrapper<Books> booksQueryWrapper = new QueryWrapper<>();
        booksQueryWrapper.eq("belong_publish_id", publishId);
        booksQueryWrapper.orderByDesc("create_at");

        if (StringUtils.hasText(typeId)) {
            booksQueryWrapper.in("id", booksIdList);
        }

        Page<Books> booksPageInfo = PageHelper.startPage(page, 4);
        List<Books> booksList = booksMapper.selectList(booksQueryWrapper);

        PageInfo<BooksDTO> pageInfo = booksPageInfo.toPageInfo(books -> {
            return getBooksById(books.getId());
        });
        return pageInfo;
    }

    private List<BooksSkuDTO> listBooksSku(String booksId) {
        QueryWrapper<BooksSku> booksSkuQueryWrapper = new QueryWrapper<BooksSku>();
        booksSkuQueryWrapper.eq("books_id", booksId);
        return booksSkuMapper.selectList(booksSkuQueryWrapper).stream().map(booksSku -> {
            return getBookSkuById(booksSku.getId());
        }).collect(Collectors.toList());
    }

    @Override
    public BooksSkuDTO getBookSkuById(String id) {
        BooksSkuDTO booksSkuDTO = new BooksSkuDTO();
        BooksSku booksSku = booksSkuMapper.selectById(id);
        if (booksSku != null) {
            BeanUtils.copyProperties(booksSku, booksSkuDTO);
            // 查询SKU图片资源
            QueryWrapper<BooksSkuImg> booksSkuImgQueryWrapper = new QueryWrapper<>();
            booksSkuImgQueryWrapper.eq("sku_id", id);
            booksSkuDTO.setBooksSkuImgList(booksSkuImgMapper.selectList(booksSkuImgQueryWrapper).stream().map(booksSkuImg -> {
                BooksSkuImgDTO booksSkuImgDTO = new BooksSkuImgDTO();
                BeanUtils.copyProperties(booksSkuImg, booksSkuImgDTO);
                return booksSkuImgDTO;
            }).collect(Collectors.toList()));
            booksSkuDTO.setOriginalPrice(booksSku.getOriginalPrice().doubleValue());
            booksSkuDTO.setCurrentPrice(booksSku.getCurrentPrice().doubleValue());
        }
        return booksSkuDTO;
    }


    @Override
    public BooksDTO getBooksBaseInfoById(String id) {

        Books books = booksMapper.selectById(id);
        BooksDTO booksDTO = new BooksDTO();
        BeanUtils.copyProperties(books, booksDTO);

        return booksDTO;
    }

}
